<?php
/**
 * 商品
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class Goods extends Model
{
    //set talbename
    protected $table = 'goods';
    //set primary key
    protected $pk = 'goods_id';

    /**
     * 添加商品
     * @param $param
     * @return false|int
     */
    public function addGoods($param)
    {
        $param['last_update'] = time();
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    /**
     * @desc 获取商品
     * @param $goods_ids
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoods($goods_ids)
    {
        $conds = [
            'goods_id' => ['in', $goods_ids]
        ];
        $field = '*';
        return $this->where($conds)->field($field)->select();
    }

    /**
     * @desc 根据条件获取商品
     * @param $conds $fields
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoodsByCond($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            //    if (in_array($key, $this->field)) {
            $arrCond[$key] = $val;
            //    }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            //    if (in_array($val, $this->field)) {
            $arrField[] = $val;
            //    }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    public function getCount($conds = [], $field = 'goods_id')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';
        return Db::table($this->table)->where($arrCond)->count($field);
    }

    /**
     * @return \think\paginator\Collection
     * 包含分页信息
     */
    public function getGoodsList($param = 'page')
    {
        $conds = [
            'is_delete' => ['=', '0'],
        ];
        if ($param == 'page') {
            return $this->where($conds)->paginate(10);
        } else {
            $fields = ['goods_id', 'goods_name'];
            return $this->where($conds)->field($fields)->select();
        }

    }

    /**
     * @param $param
     * @return false|int
     */
    public function deleteGoods($param)
    {
        $conds = [
            'goods_id' => ['=', $param],
        ];
        $data = [
            'is_delete' => 1
        ];
        return $this->allowField(true)->save($data, $conds);

    }

    /*
        public function getIsOnSaleAttr($value){
            $status = [
                '0' => '在售',
                '1' => '不在售',
            ];
            return $status[$value];
        }

        public function getIsFreeShippingAttr($value){
            $status = [
                '0' => '包邮',
                '1' => '不包邮',
            ];
            return $status[$value];
        }
    */
    public function getLastUpdateAttr($value)
    {
        return date('Y-m-d H:m:s');
    }
}
